<?php


namespace app\miniapp\controller\v1;

use app\common\cachemodel\NatalGoods;
use app\common\lib\ConfigManage;
use app\common\lib\exception\ApiException;
use app\common\model\Orders;
use app\common\model\Recode;
use app\miniapp\model\MiniVersion;
use XznsTools\cache\CacheManage;

/**
 * 星座解读
 * Class AstroExplain
 * @package app\miniapp\controller\v1
 * @author guoqiang
 */
class AstroExplain extends Auth
{
    /**
     * 星座解读
     * @version 1.0.0
     */
    public function index()
    {
        $rid = input('rid/s', '');
        $app_type = $this->headers['app-type'];

        if (!$rid)
        {
            return show(config('code.error'),'请求异常');
        }
        $recodeInfo = null;
        if ($this->user)
        {//user不为空，说明用户已经登录，并且已经创建了档案
            try
            {
                $recodeInfo = (new Recode())->getRecode(['uid' => $this->user['id'], 'id' => $rid]);
            }
            catch (\Exception $e)
            {
            }
        } else if ($app_type === 'WEB')
        {//user为空，说明没有登录，是h5，获取用户的h5档案
            $recodeInfo = CacheManage::getInstance()->setCacheConfig('miniapp.html_recode', $rid)->getCache();
        }
        if (!$recodeInfo)
        {
            return show(406,'当前档案不存在');
        }


        $astroExplainData = (new \app\web\service\AstroExplain())->getAstroExplain($recodeInfo);

        $smrData = $astroExplainData['smrData'];
        $data = $astroExplainData['data'];
        $goodType = $app_type === 'TT' ? 13 : 7; // 抖音小程序获取的为精简版本命报告 7=>完整版本命报告 13=>精简版本命报告
        if ($this->user)
        {
            $uid = (int)$this->user['id'];
            $rid = (int)$rid;
        } else
        {
            $rid = 0;
            $uid = 0;
        }
        $goodContent = \app\miniapp\common\NatalReport::getGoods($uid, $rid, $goodType, 0);

        //如果是微信小程序，并且是ios系统，则状态为4，不让用户点击购买
        if ($app_type === 'WEAPP' && (isset($this->headers['os']) && $this->headers['os'] === 'ios'))
        {
            $map = [
                'version_id' => $this->headers['version'],
                'mini_type' => 'WEAPP',
                'app_type' => 1,
                'status' => 1
            ];
            $btn_desc = (new MiniVersion())->getMiniAppBtnInfo($map, ['weapp_btn_desc']);
            foreach ($goodContent[0]['goods_list'] as $key => $value)
            {
                if ($goodContent[0]['goods_list'][$key]['status'] != 2 && !empty($btn_desc['weapp_btn_desc']))
                {
                    $goodContent[0]['goods_list'][$key]['status'] = 4;
                    $goodContent[0]['goods_list'][$key]['btn_hint'] = $btn_desc['weapp_btn_desc'] ?? '由于相关规范，iOS功能暂不可用';
                    $goodContent[0]['goods_list'][$key]['btn_desc'] = $btn_desc['weapp_btn_desc'] ?? '由于相关规范，iOS功能暂不可用';
                }
            }
        }

        $resData = [
            'smr_data' => $smrData,
            'data' => $data,
            'goods_content' => $goodContent,
        ];

        if ($app_type === 'TT')
        {
            $resData['tt_share_data'] = config('miniapp_tt_share.astro_explain');
        }

        return show(config('code.success'), '请求成功', $resData);
    }
}
